<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\model;

class CarPrice extends \app\BaseModel
{
    protected $name = "massage_service_car_price";
    protected $append = ["city_name", "cash_setting_day", "cash_setting_night"];
    public function getCashSettingDayAttr($value, $data)
    {
        if(isset($data["id"])) {
            $type_model = new CarCashType();
            return $type_model->getConfigList($data["id"], 1);
        }
    }
    public function getCashSettingNightAttr($value, $data)
    {
        if(isset($data["id"])) {
            $type_model = new CarCashType();
            return $type_model->getConfigList($data["id"], 2);
        }
    }
    public function getCityNameAttr($value, $data)
    {
        if(!empty($data["city_id"])) {
            $city_model = new City();
            $name = $city_model->where(["id" => $data["city_id"]])->value("title");
            return $name;
        }
    }
    public function dataAdd($data)
    {
        $data["create_time"] = time();
        $res = $this->insert($data);
        return $res;
    }
    public function dataUpdate($dis, $data)
    {
        $res = $this->where($dis)->update($data);
        return $res;
    }
    public function dataList($dis, $page)
    {
        $data = $this->where($dis)->order("id desc")->paginate($page)->toArray();
        return $data;
    }
    public function dataInfo($dis)
    {
        $data = $this->where($dis)->find();
        if(empty($data)) {
            $this->dataAdd($dis);
            $data = $this->where($dis)->find();
        }
        return !empty($data) ? $data->toArray() : [];
    }
    public function getCityConfigOld($uniacid, $city_id)
    {
        $dis = ["uniacid" => $uniacid, "city_id" => $city_id, "status" => 1];
        $data = $this->where($dis)->find();
        if(empty($data)) {
            $city_model = new City();
            $city_id = $city_model->where(["id" => $city_id, "city_type" => 2])->value("pid");
            $dis = ["uniacid" => $uniacid, "city_id" => $city_id, "status" => 1];
            $data = $this->where($dis)->find();
        }
        if(!empty($data)) {
            $data = $data->toArray();
        } else {
            $data = $this->dataInfo(["uniacid" => $uniacid]);
        }
        return $data;
    }
    public function getCityConfig($uniacid, $city_id, $order_start_time = 0)
    {
        $dis = ["uniacid" => $uniacid, "city_id" => $city_id, "status" => 1];
        $data = $this->where($dis)->find();
        if(empty($data)) {
            $city_model = new City();
            $city_id = $city_model->where(["id" => $city_id, "city_type" => 2])->value("pid");
            $dis = ["uniacid" => $uniacid, "city_id" => $city_id, "status" => 1];
            $data = $this->where($dis)->find();
        }
        if(!empty($data)) {
            $data = $data->toArray();
        } else {
            $data = $this->dataInfo(["uniacid" => $uniacid, "city_id" => 0]);
        }
        if(empty($data)) {
            $data = $this->dataInfo(["uniacid" => $uniacid, "city_id" => 0]);
        }
        $config = longbingGetAppConfig($uniacid);
        $order_start_time -= $config["time_interval"] * 60;
        $day = strtotime(date("Y-m-d", $order_start_time));
        $now = strtotime(date("Y-m-d", time()));
        $start_time = strtotime($data["car_price_start"]) - $now + $day;
        $end_time = strtotime($data["car_price_end"]) - $now + $day;
        if($end_time <= $start_time) {
            if($order_start_time < $end_time) {
                $start_time -= 86400;
            } else {
                $end_time += 86400;
            }
        }
        if($start_time <= $order_start_time && $order_start_time <= $end_time) {
            $data["day_type"] = 1;
        } else {
            $data["day_type"] = 2;
        }
        return $data;
    }
}

?>